package cn.lzb.book.server.entity;

import cn.lzb.book.server.common.exception.BaseException;
import lombok.Data;

/**
 * @author liuzhibo
 * @date 2024/8/28 15:54
 **/
@Data
public class BaseResult<T> {

    private Integer code;
    private String message;
    private T data;
    private Long timestamp;


    public BaseResult(String message) {
        this.code = 200;
        this.message = message;
        this.timestamp = System.currentTimeMillis();
    }

    public BaseResult(Integer code, String message) {
        this.code = code;
        this.message = message;
        this.timestamp = System.currentTimeMillis();
    }

    public BaseResult(T data) {
        this.code = 200;
        this.message = "success";
        this.data = data;
        this.timestamp = System.currentTimeMillis();
    }

    public static <T> BaseResult<T> success(T data) {
        return new BaseResult<>(data);
    }

    public static <T> BaseResult<T> success() {
        return new BaseResult<>("success");
    }

    public static <T> BaseResult<T> failure(Exception e) {
        return new BaseResult<>(500, e.getMessage());
    }

    public static <T> BaseResult<T> failure(BaseException e) {
        return new BaseResult<>(e.getCode(), e.getMessage());
    }
}

